using System;
using System.Data;
using System.Data.Common;
using System.Configuration;
using System.Data.SqlClient;

using SIS.ENT;

/// <summary>
/// The TeacherSalaryTransTableDB class is responsible for interacting with the database to retrieve and store information
/// about TeacherSalaryTransTable objects.
/// </summary>
public class TeacherSalaryTransTableDB

{
#region "Public Methods"

/// <summary>
/// Gets an instance of TeacherSalaryTransTable from the underlying datasource.
/// </summary>
/// <param name="teacherSalaryTransKey">The unique TeacherSalaryTransKey of the TeacherSalaryTransTable in the database.</param>
/// <returns>An TeacherSalaryTransTable when the TeacherSalaryTransKey was found in the database, or null otherwise.</returns>
public static TeacherSalaryTransTable GetItem(long teacherSalaryTransKey)
{
TeacherSalaryTransTable myTeacherSalaryTransTable = null;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTransTableSelectSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@teacherSalaryTransKey", teacherSalaryTransKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.Read()) {
myTeacherSalaryTransTable = FillDataRecord(myReader);
}
myReader.Close();
}
myConnection.Close();
}
return myTeacherSalaryTransTable;
}
}

/// <summary>
/// Returns a list with TeacherSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTransTable objects.</returns>
public static TeacherSalaryTransTableList GetList(int SchoolKey)
{
TeacherSalaryTransTableList tempList = new TeacherSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTransTableSelectList", myConnection))
{
 myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@schoolKey", SchoolKey);

myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
}
return tempList;
}

/// <summary>
/// Returns a list with TeacherSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTransTable objects.</returns>
public static TeacherSalaryTransTableList GetListByCurrencyCode(Int16 CurrencyCode)
{
TeacherSalaryTransTableList tempList = new TeacherSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTransTableSelectListByCurrencyCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@currencyCode", CurrencyCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TeacherSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTransTable objects.</returns>
public static TeacherSalaryTransTableList GetListByPaymentTypeCode(Int16 PaymentTypeCode)
{
TeacherSalaryTransTableList tempList = new TeacherSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTransTableSelectListByPaymentTypeCode", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@paymentTypeCode", PaymentTypeCode);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Returns a list with TeacherSalaryTransTable objects.
/// </summary>
/// <returns>A generics List with the TeacherSalaryTransTable objects.</returns>
public static TeacherSalaryTransTableList GetListByUserAccountKey(Int32 UserAccountKey)
{
TeacherSalaryTransTableList tempList = new TeacherSalaryTransTableList();
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTransTableSelectListByUserAccountKey", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;
myCommand.Parameters.AddWithValue("@userAccountKey", UserAccountKey);
myConnection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
if (myReader.HasRows)
{
while (myReader.Read())
{
tempList.Add(FillDataRecord(myReader));
}
}
myReader.Close();
}
}
return tempList;
}
}

/// <summary>
/// Saves a TeacherSalaryTransTable in the database.
/// </summary>
/// <param name="myTeacherSalaryTransTable">The TeacherSalaryTransTable instance to save.</param>
/// <returns>The new TeacherSalaryTransKey if the TeacherSalaryTransTable is new in the database or the existing TeacherSalaryTransKey when an item was updated.</returns>
public static int Save(TeacherSalaryTransTable myTeacherSalaryTransTable)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTransTableInsertUpdateSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

if (myTeacherSalaryTransTable.TeacherSalaryTransKey == -1)
{myCommand.Parameters.AddWithValue("@teacherSalaryTransKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@teacherSalaryTransKey", myTeacherSalaryTransTable.TeacherSalaryTransKey);
}
if (myTeacherSalaryTransTable.UserAccountKey == null){
myCommand.Parameters.AddWithValue("@userAccountKey", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@userAccountKey", myTeacherSalaryTransTable.UserAccountKey);
}
if (myTeacherSalaryTransTable.PaymentTypeCode == null){
myCommand.Parameters.AddWithValue("@paymentTypeCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@paymentTypeCode", myTeacherSalaryTransTable.PaymentTypeCode);
}
if (myTeacherSalaryTransTable.CurrencyCode == null){
myCommand.Parameters.AddWithValue("@currencyCode", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@currencyCode", myTeacherSalaryTransTable.CurrencyCode);
}
if (myTeacherSalaryTransTable.TransactionDate == null){
myCommand.Parameters.AddWithValue("@transactionDate", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@transactionDate", myTeacherSalaryTransTable.TransactionDate);
}
if (myTeacherSalaryTransTable.Amount == null){
myCommand.Parameters.AddWithValue("@amount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@amount", myTeacherSalaryTransTable.Amount);
}
if (myTeacherSalaryTransTable.AdditionalAmount == null){
myCommand.Parameters.AddWithValue("@additionalAmount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@additionalAmount", myTeacherSalaryTransTable.AdditionalAmount);
}
if (myTeacherSalaryTransTable.OthersAmount == null){
myCommand.Parameters.AddWithValue("@othersAmount", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@othersAmount", myTeacherSalaryTransTable.OthersAmount);
}
if (myTeacherSalaryTransTable.Remarks == null){
myCommand.Parameters.AddWithValue("@remarks", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@remarks", myTeacherSalaryTransTable.Remarks);
}
if (myTeacherSalaryTransTable.Status == null){
myCommand.Parameters.AddWithValue("@status", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@status", myTeacherSalaryTransTable.Status);
}
if (myTeacherSalaryTransTable.CreatedDateTime == null){
myCommand.Parameters.AddWithValue("@createdDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@createdDateTime", myTeacherSalaryTransTable.CreatedDateTime);
}
if (myTeacherSalaryTransTable.ModifiedDateTime == null){
myCommand.Parameters.AddWithValue("@modifiedDateTime", DBNull.Value);
}
else
{
myCommand.Parameters.AddWithValue("@modifiedDateTime", myTeacherSalaryTransTable.ModifiedDateTime);
}

 
DbParameter returnValue;
returnValue = myCommand.CreateParameter();
returnValue.Direction = ParameterDirection.ReturnValue;
myCommand.Parameters.Add(returnValue);

myConnection.Open();
myCommand.ExecuteNonQuery();
result = Convert.ToInt32(returnValue.Value);
myConnection.Close();
}
}
return result;
}

/// <summary>
/// Deletes a TeacherSalaryTransTable from the database.
/// </summary>
/// <param name="teacherSalaryTransKey">The TeacherSalaryTransKey of the TeacherSalaryTransTable to delete.</param>
/// <returns>Returns true when the object was deleted successfully, or false otherwise.</returns>
public static bool Delete(long teacherSalaryTransKey)
{
int result = 0;
using (SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["sis.dbconn"].ConnectionString))
{
using (SqlCommand myCommand = new SqlCommand("sprocTeacherSalaryTransTableDeleteSingleItem", myConnection))
{
myCommand.CommandType = CommandType.StoredProcedure;

myCommand.Parameters.AddWithValue("@teacherSalaryTransKey", teacherSalaryTransKey);
myConnection.Open();
result = myCommand.ExecuteNonQuery();
myConnection.Close();
}
}
return result > 0;
}

#endregion

/// <summary>
/// Initializes a new instance of the TeacherSalaryTransTable class and fills it with the data fom the IDataRecord.
/// </summary>
private static TeacherSalaryTransTable FillDataRecord(IDataRecord myDataRecord )
{
TeacherSalaryTransTable myTeacherSalaryTransTable = new TeacherSalaryTransTable();
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TeacherSalaryTransKey")))
{
myTeacherSalaryTransTable.TeacherSalaryTransKey = myDataRecord.GetInt64(myDataRecord.GetOrdinal("TeacherSalaryTransKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("UserAccountKey")))
{
myTeacherSalaryTransTable.UserAccountKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("UserAccountKey"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("PaymentTypeCode")))
{
myTeacherSalaryTransTable.PaymentTypeCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("PaymentTypeCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CurrencyCode")))
{
myTeacherSalaryTransTable.CurrencyCode = myDataRecord.GetInt16(myDataRecord.GetOrdinal("CurrencyCode"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("TransactionDate")))
{
myTeacherSalaryTransTable.TransactionDate = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("TransactionDate"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Amount")))
{
myTeacherSalaryTransTable.Amount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("Amount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("AdditionalAmount")))
{
myTeacherSalaryTransTable.AdditionalAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("AdditionalAmount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OthersAmount")))
{
myTeacherSalaryTransTable.OthersAmount = myDataRecord.GetDecimal(myDataRecord.GetOrdinal("OthersAmount"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Remarks")))
{
myTeacherSalaryTransTable.Remarks = myDataRecord.GetString(myDataRecord.GetOrdinal("Remarks"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("Status")))
{
myTeacherSalaryTransTable.Status = myDataRecord.GetInt16(myDataRecord.GetOrdinal("Status"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("CreatedDateTime")))
{
myTeacherSalaryTransTable.CreatedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("CreatedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("ModifiedDateTime")))
{
myTeacherSalaryTransTable.ModifiedDateTime = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("ModifiedDateTime"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("FirstName")))
{
    myTeacherSalaryTransTable.FirstName = myDataRecord.GetString(myDataRecord.GetOrdinal("FirstName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("LastName")))
{
    myTeacherSalaryTransTable.LastName = myDataRecord.GetString(myDataRecord.GetOrdinal("LastName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("OtherName")))
{
    myTeacherSalaryTransTable.OtherName = myDataRecord.GetString(myDataRecord.GetOrdinal("OtherName"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("DateOfBirth")))
{
    myTeacherSalaryTransTable.DateOfBirth = myDataRecord.GetDateTime(myDataRecord.GetOrdinal("DateOfBirth"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("NRICNumber")))
{
    myTeacherSalaryTransTable.NRICNumber = myDataRecord.GetString(myDataRecord.GetOrdinal("NRICNumber"));
}
if (!myDataRecord.IsDBNull(myDataRecord.GetOrdinal("SchoolKey")))
{
    myTeacherSalaryTransTable.SchoolKey = myDataRecord.GetInt32(myDataRecord.GetOrdinal("SchoolKey"));
}
return myTeacherSalaryTransTable;
}
}
